﻿using System;
using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Model;
using HsTec.Mobile.Green.Util;

public partial class PowerWarning_ProvinceForecastEdit : System.Web.UI.Page
{
    ProvinceForecastBLL bll = new ProvinceForecastBLL();
    public string sEditStatus = "1";
    private string sID = String.Empty;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            try
            {
                sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
                if (sEditStatus == "2")
                {
                    sID = this.Request.QueryString["ID"].ToString().Trim();
                    ProvinceForecast pf = bll.GetModel(int.Parse(sID));
                    sltYear.Value = pf.Years.Substring(0, 4);
                    sltMonth.SelectedValue = pf.Years.Substring(4, 2);
                    txtValue.Text = pf.Value.ToString();
                }
                else
                {
                }
            }
            catch
            {
            }
        }
    }

    protected void SaveData_ServerClick(object sender, EventArgs e)
    {
        ProvinceForecast pf = new ProvinceForecast();
        pf.Years = sltYear.Value + sltMonth.SelectedValue;
        try
        {
            pf.Value = Decimal.Parse(txtValue.Text);
        }
        catch
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请输入数值类型数据!');</script>");
            return;
        }

        sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();

        if (sEditStatus == "1")
        {
            int iback = bll.Add(pf);
            if (iback > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');window.close();</script>");
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
                return;
            }
        }
        else
        {
            sID = this.Request.QueryString["ID"].ToString().Trim();
            pf.PF_ID = int.Parse(sID);
            int iback = bll.Update(pf);
            if (iback > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');window.close();</script>");  //window.opener.location.reload();
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                return;
            }
        }
    }
    protected void sltMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sValue = String.Empty;
        string lastYears = DateTime.Parse(sltYear.Value + "-" + sltMonth.SelectedValue + "-01").AddYears(-1).ToString("yyyy");
        if (sltMonth.SelectedValue == "01")
        {
            sValue = SQLHelper.ExecuteString("select avg(Powers) from tb_ReportAnalysis where substring(Years,1,4)='" + lastYears + "'");
            if (!String.IsNullOrEmpty(sValue))
            {
                sValue = double.Parse(sValue).ToString("0.00");
            }
            labValue.Text = "参考值：" + sValue;
        }
        else
        {
            sValue = SQLHelper.ExecuteString("select avg(Powers) from tb_ReportAnalysis where Years>='" + sltYear.Value + "01' and Years<='" + sltYear.Value + sltMonth.SelectedValue + "'");
            if (!String.IsNullOrEmpty(sValue))
            {
                sValue = double.Parse(sValue).ToString("0.00");
            }
            labValue.Text = "参考值：" + sValue;
        }
    }
}
